package com.example.hotelsystem.service.impl;

import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.mapper.RoomTypeMapper;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.server.Server;
import com.example.hotelsystem.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelsystem.vo.comment.CommentViewVO;
import com.example.hotelsystem.vo.list.ListVO;
import com.example.hotelsystem.vo.roomtype.RoomTypeViewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URLEncoder;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType> implements RoomTypeService {

    @Autowired
    RoomTypeService RoomTypeService;
    @Autowired
    RoomTypeImageService roomTypeImageService;
    @Autowired
    TCommentService tCommentService;
    @Autowired
    UserService userService;
    @Autowired
    CityService cityService;
    @Autowired
    RoomService roomService;
    @Autowired
    RoomTypeMapper roomTypeMapper;

    @Override
    @Transactional
    public RoomTypeViewVO getRoomTypeView(Long roomTypeId){
        RoomTypeViewVO vo = new RoomTypeViewVO();
        RoomType rt = RoomTypeService.getById(roomTypeId);
        BeanUtils.copyProperties(rt,vo);

        QueryWrapper<RoomTypeImage> query = new QueryWrapper<>();
        query.eq("room_type_id",rt.getId());
        query.eq("type",0);
        List<RoomTypeImage> images = roomTypeImageService.list(query);
        List<String> imageList = new ArrayList<>();
        for(RoomTypeImage item : images){
            imageList.add(item.getImage());
        }
        vo.setImageList(imageList);

        QueryWrapper<RoomTypeImage> query2 = new QueryWrapper<>();
        query2.eq("room_type_id",rt.getId());
        query2.eq("type",1);
        List<RoomTypeImage> images2 = roomTypeImageService.list(query2);
        List<String> imageList2 = new ArrayList<>();
        for(RoomTypeImage item : images2){
            imageList2.add(item.getImage());
        }
        vo.setViewList(imageList2);

        QueryWrapper<Room> roomQuery = new QueryWrapper<>();
        roomQuery.eq("roomtype_id",roomTypeId);
        List<Room> rooms = roomService.list(roomQuery);
        List<Long> cityIdList = new ArrayList<>();
        for(Room r : rooms){
            cityIdList.add(r.getCityId());
        }

        if(cityIdList!=null&&cityIdList.size()>0){
            //去除cityList中重复的数据
            Set set = new HashSet();
            List<Long> newList = new  ArrayList();
            for (Long cd:cityIdList) {
                if(set.add(cd)){
                    newList.add(cd);
                }
            }
            String name = "";
            for(Long id :newList){
                City c = cityService.getById(id);
                name+=c.getCityName()+",";
            }
            name = name.substring(0,name.length()-1);
            vo.setCityName(name);
        }


        QueryWrapper<TComment> commentQuery = new QueryWrapper<>();
        commentQuery.eq("room_type_id",rt.getId());
        commentQuery.orderByDesc("create_time");
        List<TComment> comments = tCommentService.list(commentQuery);
        List<CommentViewVO> commentList = new ArrayList<>();

        double count = 0;
        double starSum = 0;
        for(TComment item : comments){
            CommentViewVO cvvo = new CommentViewVO();
            BeanUtils.copyProperties(item,cvvo);

            User user = userService.getById(item.getUserId());
            if(item.getStar()!=5.0){
                cvvo.setStar(String.format("%.1f",item.getStar()));
            }else{
                cvvo.setStar("5");
            }
            cvvo.setAvatar(user.getAvatar());
            cvvo.setUserName(user.getUsername());
            commentList.add(cvvo);

            starSum += item.getStar();
            count++;

        }
        if(starSum/count==5.0){
            vo.setStar("5");
        }else{
            vo.setStar(String.format("%.1f",starSum/count));
        }

        vo.setCommentCount(count);
        vo.setCommentList(commentList);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result upLoadImg(MultipartFile file){
        String fileName;
        fileName = file.getOriginalFilename();
        fileName= UUID.randomUUID()+"_"+fileName;
        String dirPath = "/www/server/tomcat/webapps/roomtype/image/";
        File filePath = new File(dirPath);
        if(!filePath.exists()){
            filePath.mkdirs();
        }
        try{
            file.transferTo((new File(dirPath+fileName)));
            String encoderUrl = URLEncoder.encode(fileName, "UTF-8");
            String fileurl= "http://" + Server.iphost+":"+ Server.port + "/roomtype/image/"+encoderUrl;

                return Result.success("图片上传成功！！",fileurl);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(CodeError.INTERNAL_EXCEPTION,"上传失败"+e.getMessage());
        }
    }

    @Override
    @Transactional
    public List<ListVO> RoomTypeListVO(){
        List<ListVO> list = roomTypeMapper.RoomTypeListVO();
        return list;
    }
}
